package com.easy;

/**
 * Created by Lxk on 2019/6/3.
 */
public class Solution160 {

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA==null||headB==null){
            return null;
        }
        ListNode curA = headA;
        ListNode curB = headB;
        int countA=0,countB=0;
        while(curA!=null){
            curA = curA.next;
            countA++;
        }
        while(curB!=null){
            curB = curB.next;
            countB++;
        }
        curA = headA;
        curB = headB;
        if(countA>countB){
            int count = countA - countB;
            while(count!=0){
                curA = curA.next;
                count--;
            }
        }else{
            int count = countB - countA;
            while(count!=0){
                curB = curB.next;
                count--;
            }
        }
        while(curA!=null&&curB!=null&&curA!=curB){
            curA = curA.next;
            curB = curB.next;
        }
        return curB;
    }

}
